<?php
session_start();

$img_width = 300;
$img_height = 120;
$img_padding = 50;
$t_fontsize = 35;
$fontfamily = 'ACaslonPro-Bold.otf';
$img_pct = 20;
$session_name = 'yzm';
$s_img_text = '';
//验证码包含的字符串
//$string_array_a = array('A','B','C','D','E','F','G','H','J','K','M','N','P','Q','R','S','T','U','V','W','X','Y','Z');
$string_array_a = array('0','1','2','3','4','5','6','7','8','9');
//$string_array_l = array('a','b','c','d','e','f','g','h','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z');
$string_array_l = array('0','1','2','3','4','5','6','7','8','9');
$string_array_n = array('0','1','2','3','4','5','6','7','8','9');
$string_array = array_merge($string_array_a,$string_array_l,$string_array_n);
shuffle($string_array);
$img_text = $string_array;
//验证码长度
$img_text_len = mt_rand(4,4);

$img_width_i = (int)($img_width - $img_padding*2)/$img_text_len;

$img_paddingx = $img_padding;
$img_paddingy = $img_height-$img_padding;

$im = imagecreatetruecolor($img_width,$img_height);
$im1 = imagecreatetruecolor($img_width,$img_height);

$c_white = imagecolorallocate($im, 255, 255, 255);
$c1_white = imagecolorallocate($im1, 255, 255, 255);

$rand_r = mt_rand(0,255);
$rand_g = mt_rand(0,255);
$rand_b = mt_rand(0,255);
$c_rand = imagecolorallocate($im1, $rand_r, $rand_g, $rand_b);

imagefilledrectangle($im,0,0,$img_width,$img_height,$c_white);
imagefilledrectangle($im1,0,0,$img_width,$img_height,$c_rand);

for($img_i=0;$img_i<$img_text_len;$img_i++){

	$rand_r = mt_rand(0,0);
	$rand_g = mt_rand(0,0);
	$rand_b = mt_rand(0,0);
	$c_rand = imagecolorallocate($im, $rand_r, $rand_g, $rand_b);

	$rand_angle = mt_rand(315,405);
	imagettftext($im, $t_fontsize, $rand_angle, $img_paddingx+($img_width_i*$img_i), $img_paddingy, $c_rand, $fontfamily, $img_text[$img_i]);
	
	$rand_r = mt_rand(0,255);
	$rand_g = mt_rand(0,255);
	$rand_b = mt_rand(0,255);
	$c_rand = imagecolorallocate($im1, $rand_r, $rand_g, $rand_b);
	
	$im1_i = (int)$img_height/$img_text_len;
	
	imagefilledrectangle($im1,0,mt_rand($im1_i*$img_i,$im1_i*($img_i+1)),$img_width,$img_height,$c_rand);
			
	$s_img_text .= $img_text[$img_i];
	
}

imagecopymerge($im,$im1,0,0,0,0,$img_width,$img_height,$img_pct);

header("Expires: Sat, 1 Jan 2000 08:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");   // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);      // HTTP/1.0
header("Pragma: no-cache");
header("Content-type: image/png");

imagepng($im);
imagedestroy($im);
imagedestroy($im1);

$_SESSION[$session_name] = $s_img_text;
?>